//
//  AMPRegisterSetPwdView.swift
//  LivingHelper
//
//  Created by Ampaw on 2017/9/14.
//  Copyright © 2017年 Ampaw. All rights reserved.
//

import UIKit

protocol AMPRegisterSetPwdViewDelegate: NSObjectProtocol {
    func didRegisterAndLogin()
}

class AMPRegisterSetPwdView: UIView {
    
    // MARK: - 属性
    weak var registerSetPwdViewDelegate: AMPRegisterSetPwdViewDelegate?

    // MARK: - 构造方法
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        // 准备UI
        setUpUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - 准备UI
    /// 设置UI视图
    func setUpUI(){
        self.addSubview(self.passwordTextView)
        self.passwordTextView.addSubview(self.passwordTextField)
        self.passwordTextView.addSubview(self.passwordChageShowButton)
        self.addSubview(self.registerAndLoginButton)
        
        self.registerAndLoginButton.addTarget(self, action: #selector(registerAndLoginAction), for: UIControlEvents.touchUpInside)
        
        unowned let WS = self
        self.passwordTextView.snp.makeConstraints { (make) -> Void in
            make.centerX.equalTo(WS).offset(0)
            make.top.equalTo(WS).offset(30)
            make.width.equalTo(AMPConstants.AMP_SCREEN_WIDTH*0.8)
            make.height.equalTo(44)
        }
        
        self.passwordTextField.snp.makeConstraints { (make) -> Void in
            make.centerY.left.height.equalTo(WS.passwordTextView)
            make.right.equalTo(WS.passwordChageShowButton.snp.left)
        }
        self.passwordChageShowButton.snp.makeConstraints { (make) -> Void in
            make.centerY.right.equalTo(WS.passwordTextView)
            make.width.equalTo(40)
            make.height.equalTo(30)
        }
        
        self.registerAndLoginButton.snp.makeConstraints { (make) -> Void in
            make.centerX.equalTo(WS).offset(0)
            make.top.equalTo(WS.passwordTextView.snp.bottom).offset(20)
            make.width.height.equalTo(WS.passwordTextView)
        }
    }
    
    // MARK: - Event
    @objc private func registerAndLoginAction() {
        registerSetPwdViewDelegate?.didRegisterAndLogin()
    }
    
    @objc private func changeShowPwd(button: UIButton) {
        // 反选效果
        button.isSelected = !button.isSelected
        if button.isSelected {
            self.passwordTextField.isSecureTextEntry = true  // 暗文显示
        }else{
            self.passwordTextField.isSecureTextEntry = false // 明文显示
        }
    }
    
    // MARK: - 懒加载
    lazy var passwordTextView: UIView = {
        let authCodeView = UIView()
        authCodeView.backgroundColor = UIColor.white
        authCodeView.layer.cornerRadius = 5.0;
        authCodeView.layer.masksToBounds = true
        authCodeView.layer.borderColor = UIColor.white.cgColor
        authCodeView.layer.borderWidth = 1.0
        
        return authCodeView
    }()
    
    lazy var passwordTextField: UITextField = {
        let password = UITextField()
        password.backgroundColor = UIColor.white
        password.layer.cornerRadius = 5.0;
        password.layer.masksToBounds = true
        password.layer.borderColor = UIColor.white.cgColor
        password.layer.borderWidth = 1.0
        password.font = AMPConstants.AMP_Font14
        password.placeholder = "请输入登陆密码"
        // 注意：不能同时设置clearButtonMode、rightViewMode/rightView属性，否则只有rightViewMode/rightView有效。
        password.clearButtonMode = UITextFieldViewMode.whileEditing
        password.autocapitalizationType = UITextAutocapitalizationType.none
        password.isSecureTextEntry = false // 设置密码，明文显示
        
        let leftImg = UIImageView()
        leftImg.frame = CGRect(x: 0, y: 0, width: 30, height: 30)
        leftImg.image = UIImage(named: "login_icon_password")
        password.leftView = leftImg
        password.leftViewMode = UITextFieldViewMode.always
        
        return password
    }()
    
    lazy var passwordChageShowButton: UIButton = {
        let button = UIButton()
        button.backgroundColor = UIColor.clear
        button.setImage(UIImage(named: "icon_visible_normal"), for: UIControlState.normal)
        button.setImage(UIImage(named: "icon_visible_disabled"), for: UIControlState.selected)
        button.addTarget(self, action: #selector(changeShowPwd(button:)), for: UIControlEvents.touchUpInside)
        return button
    }()
    
    private lazy var registerAndLoginButton: UIButton = {
        let button = UIButton()
        button.backgroundColor = AMPConstants.AMPMainColor
        button.layer.cornerRadius = 5.0;
        button.layer.masksToBounds = true
        button.titleLabel?.font = AMPConstants.AMP_Font18
        button.setTitle("注册并登录", for: UIControlState.normal)
        button.setTitleColor(UIColor.white, for: UIControlState.normal)
        button.setTitleColor(UIColor.black, for: UIControlState.highlighted)
        
        return button
    }()

}
